﻿using Ausy.Silverlight.Survey.AccountModule.Model;
using Ausy.Silverlight.Survey.AccountModule.Services;
using Ausy.Silverlight.Survey.AccountModule.ViewParts;
using Ausy.Silverlight.Survey.Infrastructure.Regions;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace Ausy.Silverlight.Survey.AccountModule
{
    /// <summary>
    /// Cette classe représente un Module de gestion des comptes
    /// </summary>
    public class AccountModule : IModule
    {
        #region Private Members

        /// <summary>
        /// Container Unity pour instanciation des type d'objet
        /// </summary>
        private readonly IUnityContainer container;

        /// <summary>
        /// Registre des vues à afficher
        /// </summary>
        private readonly IRegionViewRegistry regionViewRegistry;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        /// <param name="container">Container Unity pour instanciation des type d'objet</param>
        /// <param name="regionViewRegistry">Registre des vues à afficher</param>
        public AccountModule(IUnityContainer container, IRegionViewRegistry regionViewRegistry)
        {
            this.container = container;
            this.regionViewRegistry = regionViewRegistry;
        }

        #endregion

        #region Initialize Method

        /// <summary>
        /// Intialisation du module principal
        /// </summary>
        public void Initialize()
        {
            this.RegisterTypesAndServices();

            this.RegisterViewWithRegions();
        }

        #endregion

        #region RegisterTypesAndServices Method

        /// <summary>
        /// Enregistrement des type et des services dans le container IoC
        /// </summary>
        private void RegisterTypesAndServices()
        {
            this.container.RegisterType<IAccountService, AccountService>(new ContainerControlledLifetimeManager());
            this.container.RegisterType<Account>();

            this.container.RegisterType<ICreateAccountViewModel, CreateAccountViewModel>();
            this.container.RegisterType<ICreateAccountView, CreateAccountView>();
            this.container.RegisterType<IExistingAccountViewModel, ExistingAccountViewModel>();
            this.container.RegisterType<IExistingAccountView, ExistingAccountView>();
        }

        #endregion

        #region RegisterViewWithRegions Method

        /// <summary>
        /// Enregistrement dans les regions d'affichage
        /// </summary>
        private void RegisterViewWithRegions()
        {
            this.regionViewRegistry.RegisterViewWithRegion(RegionNames.CreateAccountRegion, () => this.container.Resolve<ICreateAccountViewModel>().View);
            this.regionViewRegistry.RegisterViewWithRegion(RegionNames.ExistingAccountRegion, () => this.container.Resolve<IExistingAccountViewModel>().View);
        }

        #endregion

    }
}
